package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")QueryObject qo, Model model){
        return "region/list";
    }

    //查询列表的treeview标签
    @RequestMapping("/getRegionsByParentId")
    @ResponseBody
    public Object getRegionsByParentId(Long parentId){
        //查询出来的列表
       List<Region> list =  regionService.getRegionsByParentId(parentId);
       List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
       //将数据转换成指定的格式返回
        for (Region region : list) {
            mapList.add(region.getString2Map());
        }
        return mapList;
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Region region){
        JSONResult jsonResult = new JSONResult();
        try {
            regionService.saveOrUpdate(region);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;

    }
}
